<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <script src="../../js/load.js"></script>
</head>

<body>
<div class="layui-fluid">
    <div class="flexrow">
        <div class="layui-panel list-container">
            <div class="list-search-container">
                <button class="layui-btn layui-btn-sm" onclick="HandleUpload()"><i
                        class="layui-icon layui-icon-upload-circle layui-font-12"></i>上传配置文件
                </button>
            </div>
            <table class="layui-table" lay-data="{id: 'datagrid',height:'full'}" lay-filter="datagrid">
                <thead>
                <tr>
                    <th lay-data="{field:'fileName'}">文件名称</th>
                    <th lay-data="{field:'createDate',width:180,align:'center'}">创建时间</th>
                    <th lay-data="{field:'lastModifyDate',width:180,align:'center'}">修改时间</th>
                    <th lay-data="{fixed: 'right', width:260, align:'center', toolbar: '#op'}">操作</th>
                </tr>
                </thead>
            </table>
            <div style="display: none;">
                <input type="file" name="file" id="file" style="display:none" onchange="HandleRealUpload()"/>
            </div>
        </div>
    </div>
</div>
</body>
<script type="text/html" id="op">
    <button class="layui-btn layui-btn-sm" lay-event="HandleDownLoad"><i
            class="layui-icon layui-icon-download-circle layui-font-12"></i>下载
    </button>
    <button class="layui-btn layui-btn-sm" lay-event="HandleEdit"><i
            class="layui-icon layui-icon-edit layui-font-12"></i>编辑
    </button>
    <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="HandleDelete"><i
            class="layui-icon layui-icon-delete layui-font-12"></i>删除
    </button>
</script>
</html>
<script>
    $(function () {
        layui.use('laytpl', function () {
        })
        BindTableData();
    })

    function BindTableData() {
        var param = {
            projectID: GetUrlParam("projectID")
        }
        BindTable("datagrid", "api/config/list", param, function (result) {
            var fileExt = result.reponse.data.fileExt;
            $("#file").attr("accept", fileExt);
        }, false);
    }


    function HandleUpload() {
        $("#file").click();
    }

    function HandleRealUpload() {
        var formData = new FormData();
        var file = document.getElementById("file").files[0];
        formData.append("file", file, file.name);
        formData.append("projectID", GetUrlParam("projectID"))
        HttpForm(formData, "api/config/upload", function (result) {
            if (result.code == 0) {
                OpenSuccessMessage("上传成功")
                BindTableData();
            }
        })
    }

    function HandleEdit(RowData) {
        OpenDialog('编辑配置文件', 'pages/config/config_edit.html?projectID=' + GetUrlParam("projectID") + "&fileName=" + encodeURIComponent(RowData.fileName), 1366, 768)
    }

    function HandleDelete(RowData) {
        OpenConfirm('确认删除？', function () {
            var param = {
                projectID: GetUrlParam("projectID"),
                fileName: RowData.fileName
            }
            HttpPost(JSON.stringify(param), 'api/config/delete', function (result) {
                if (result.code == 0) {
                    OpenSuccessMessage("删除成功")
                    BindTableData();
                } else {
                    OpenFail(result.msg);
                }
            })
        })
    }
    
    function HandleDownLoad(RowData) {
        var url = GetRootPath()+"api/config/download?projectID="+GetUrlParam("projectID")+"&fileName="+encodeURIComponent(RowData.fileName);
        window.top.open(url);
    }
</script>